package com.bjfu.gmms.servlet.departmentHeadServlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.PrimaryDisciplineInfoMapper;
import com.bjfu.gmms.mapper.ResearchDirectionMapper;
import com.bjfu.gmms.mapper.SecondaryDisciplineStaticInfoMapper;
import com.bjfu.gmms.model.ResearchDirection;
import com.bjfu.gmms.pojo.PrimaryDisciplineInfo;
import com.bjfu.gmms.pojo.SecondaryDisciplineStaticInfo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/department_head_view/*")
public class DepartmentHeadPageRedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        SqlSession sqlSession = null;
        HttpSession session = request.getSession();
        int i_collegeId = Integer.parseInt(session.getAttribute("collegeId").toString());
        String collegeId = String.format("%03d", i_collegeId);

        try {
            // 获取 SqlSession
            sqlSession = MyBatisUtil.getSqlSession("development");
            PrimaryDisciplineInfoMapper mapper = sqlSession.getMapper(PrimaryDisciplineInfoMapper.class);
            SecondaryDisciplineStaticInfoMapper mapper2 = sqlSession.getMapper(SecondaryDisciplineStaticInfoMapper.class);
            ResearchDirectionMapper mapper3 = sqlSession.getMapper(ResearchDirectionMapper.class);

            // 执行查询
            List<PrimaryDisciplineInfo> p1_list = mapper.selectByCollegeId(collegeId);
            List<PrimaryDisciplineInfo> pdi_list = new ArrayList<>();   // 初始化 all_list
            List<SecondaryDisciplineStaticInfo> p2_list = mapper2.selectAllSecondaryDisciplineStaticInfoByCollegeId(collegeId);  // 辅助用
            List<SecondaryDisciplineStaticInfo> sdi_list = new ArrayList<>();  // 初始化 d3 列表
            // 遍历每个一级学科
            for (PrimaryDisciplineInfo primaryDisciplineInfo : p1_list) {
                List<SecondaryDisciplineStaticInfo> secondaryDisciplineStaticInfos = mapper2.selectAllSecondaryDisciplineStaticInfoByPrimaryDisciplineId(primaryDisciplineInfo.getPrimaryDisciplineId());
                // 遍历每个二级学科
                for (SecondaryDisciplineStaticInfo secondaryDiscipline : secondaryDisciplineStaticInfos) {
                    List<ResearchDirection> researchDirections = mapper3.getResearchDirectionsByDiscipline(secondaryDiscipline.getSecondaryDisciplineId());
                    secondaryDiscipline.setResearchDirections(researchDirections);  // 设置研究方向到二级学科对象中
                }
                primaryDisciplineInfo.setSecondaryDisciplineStaticInfos(secondaryDisciplineStaticInfos);
                pdi_list.add(primaryDisciplineInfo);
            }

            for (SecondaryDisciplineStaticInfo secondaryDiscipline : p2_list){
                List<ResearchDirection> researchDirections = mapper3.getResearchDirectionsByDiscipline(secondaryDiscipline.getSecondaryDisciplineId());
                secondaryDiscipline.setResearchDirections(researchDirections);
                sdi_list.add(secondaryDiscipline);
            }


            // 获取请求的URI
            String uri = request.getRequestURI();
            if (uri.contains("disciplineList")) {
                // 将查询结果存入 session
                session.setAttribute("pdi_list", pdi_list);
                request.getRequestDispatcher("/WEB-INF/department_head_view/disciplineList.jsp").forward(request, response);
            } else if (uri.contains("editPrimaryDiscipline.jsp")) {
                // 转发到一级学科列表界面
                session.setAttribute("pdi_list", pdi_list);
                request.getRequestDispatcher("/WEB-INF/department_head_view/editPrimaryDiscipline.jsp").forward(request, response);
            } else if (uri.contains("editSecondaryDiscipline.jsp")) {
                // 转发到二级学科列表界面
                session.setAttribute("pdi_list", pdi_list);
                request.getRequestDispatcher("/WEB-INF/department_head_view/editSecondaryDiscipline.jsp").forward(request, response);
            } else if (uri.contains("editResearchDirection.jsp")){
                // 转发到研究方向列表界面
                session.setAttribute("secondaryDisciplines", sdi_list);
                request.getRequestDispatcher("/WEB-INF/department_head_view/editResearchDirection.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();  // 确保关闭 SqlSession
            }
        }
    }
}
